package me;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author yexy
 * @date 2020/6/30 11:04 PM
 * @description
 */
public class ChatClient {

    public static final String IP = "127.0.01";
    public static final Integer PORT = 9999;

    public static void main(String[] args) {
        Socket socket = null;
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedReader bufferedReader = null;
        try {
            socket = new Socket(IP, PORT);
            Scanner scanner = new Scanner(System.in);

            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
            objectInputStream = new ObjectInputStream(socket.getInputStream());
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            boolean flag = true;
            do {
                System.out.println("请选择进行的操作：1 聊天  ｜｜   2上传文件    ｜｜ 3 退出");
                int opt = scanner.nextInt();
                if (opt == 3) {
                    Message message = new Message(3, "");
                    objectOutputStream.writeObject(message);

                    flag = false;
                } else {
                    if (opt == 1) {
                        System.out.println("请输入聊天内容：");
                        String content = scanner.next();
                        Message message = new Message(1, content);
                        objectOutputStream.writeObject(message);
                    } else {
                        System.out.println("服务器建立文件上传连接成功，请输入文件位置");
                        String path = scanner.next();

                        File file = new File(path);
                        Message message = new Message(2, file.getName());
                        objectOutputStream.writeObject(message);
                        if (file.isFile()) {
                            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
//                            System.out.println(file.length());
//                            byte[] bytes = new byte[(int) file.length()];//存储每次读取的数据
//                            message.setFileContent(bytes);
//                            bufferedInputStream.read(bytes);
//                            message.setFileContent(bytes);

                            byte[] bytes = new byte[1024];
                            int count = 0;
                            //发送文件
                            while ((count = bufferedInputStream.read(bytes)) != -1) {
                                bufferedOutputStream.write(bytes, 0, count);
                                bufferedOutputStream.flush();
                            }

                        } else {
                            System.out.println("仅支持文件上传！");
                        }
                    }

                    //System.out.println("服务器端返回--------：" + ((Message) objectInputStream.readObject()).getMessage());
//                    System.out.println(bufferedReader.readLine());
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        System.out.println(line);
                    }


                }

            } while (flag);

            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
